package org.dxy.trigger.listener.route;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.eventbus.Subscribe;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.domain.event.route.RouteDisabledEvent;
import org.dxy.domain.model.valobj.RouteStatusEnum;
import org.dxy.infrastructure.entity.query.RouteViewEntity;
import org.dxy.infrastructure.persistent.service.RouteViewService;
import org.dxy.trigger.listener.EventListener;
import org.springframework.stereotype.Component;

/**
 * 工艺路线禁用事件监听器
 * @author: dxy
 * @createTime: 2025/8/17 11:49
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RouteDisabledEventListener implements EventListener<RouteDisabledEvent> {
    
    private final RouteViewService routeViewService;
    
    @Override
    @Subscribe
    public void onEvent(RouteDisabledEvent event) {
        try {
            log.info("处理工艺路线禁用事件: {}", event);

            LambdaUpdateWrapper<RouteViewEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(RouteViewEntity::getId, event.routeId())
                    .set(RouteViewEntity::getStatus, RouteStatusEnum.DISABLED.getKey());

            routeViewService.update(updateWrapper);
            
            log.info("工艺路线读模型状态更新成功, routeId={}, status={}", 
                    event.routeId(), RouteStatusEnum.DISABLED.getKey());
        } catch (Exception e) {
            log.error("处理工艺路线禁用事件失败", e);
            throw new RuntimeException("处理工艺路线禁用事件失败", e);
        }
    }
}